<?php
// +----------------------------
// | Created by PhpStorm
// +----------------------------
// | Author: YnnSon
// +----------------------------
namespace app\api\model;

use app\api\service\Token;
use think\Db;

/**
 * 奖品
 * Class Gift
 * @package app\api\model
 */
class Gift extends BaseModel
{
    /**
     * @Desc 为图片添加URL前缀
     *
     * @param $value
     *
     * @return string
     */
    protected function getGiftImgAttr($value)
    {
        return $value ? $this->addWebUrlPrefixToImage($value) : $value;
    }

    /**
     * @Desc 获取奖品列表
     *
     * @param $status
     *
     * @return array
     */
    public static function getGiftList($status)
    {
        $open_id = Token::getOpenidFromCache();
        $list = [];
        $orders = Db::name('order')->field('create_time, activity_id, order_id')->where('open_id', '=', $open_id)->where('is_get_gift', '=', $status)->where('order_status', '<>', 4)->order('update_time desc')->select();
        foreach ($orders as $key => $order) {
            $activitys = Db::name('activity')->field('gift_id, activity_name')->where('id', '=', $order['activity_id'])->where('is_use', '=', 1)->where('is_delete', '=', 0)->select();
            foreach ($activitys as $activity) {
                $ids = explode(",", $activity['gift_id']);
                foreach ($ids as $idsKey => $id) {
                    $gifts = (new self)->field('id, gift_name, gift_img, gift_price')->where('id', '=', $id)->where('is_delete', '=', 0)->select();
                    foreach ($gifts as $gift) {
                        $list[$key]['gift_list'][$idsKey] = $gift;
                    }
                    $list[$key]['activity_name'] = $activity['activity_name'];
                    $list[$key]['create_time'] = date("Y-m-d", $order['create_time']);
                    $list[$key]['order_id'] = $order['order_id'];
                }
            }
        }
        return $list;
    }
}